function videoHub() {
	
}

videoHub.prototype.settings = {
	videoGrab:30,
	videosPerTab:6,
	queryOptions:""
}
videoHub.prototype.libraries = new Array();

//Que up playlists to be added to accordian selector
videoHub.prototype.addLibrary = function(librarySettings) { 
	var problem = false;
	
	var d = librarySettings;    //{name:libName, user:youtubeUser, playlist:youtubePlaylist};
	if (librarySettings.name) {
		d.name = librarySettings.name;
	} else {
		//console.log("You must specify a library name.\n");
		problem = true;
	}
	if ((librarySettings.user && librarySettings.playlist)) {
		d.user = librarySettings.user;
		d.playlist = librarySettings.playlist;
	} else if (librarySettings.url) {
		d.url = librarySettings.url;
	} else {
		//console.log("You must specify either a username/playlist combination or a full youtube api url.\n");
		problem = true;
	}
	
	if (!problem) {
		this.libraries.push(d);
	}
};

//Create videoHubDB if needed, Fetch all data, add to videoHubDB, create canvas div elements, paint divs
videoHub.prototype.init = function(targetDiv) {
	if (typeof(videoHubController)!="object") {
		videoHubController = {
			showEmbedCode: function() {
				tb_show("Copy to embed", "#TB_inline?height=300&width=300&inlineId=videoHub-current-embed");
				
			},
			sendEmail:function(mailtoLink) {
				win = window.open(mailtoLink,'emailWindow');
				if (win && win.open &&!win.closed) win.close();
					
			}
		}
	}
	
	//Create object to handle the views
	if (typeof(videoHubView)!="object") {
		videoHubView = {
			targetDiv:$("#"+targetDiv),
			targetList:"",
			targetCurrent:"",
			libsLoaded:0,
			accordionFlag:0,
			init: function() {
				//alert(this.targetDiv);
				this.targetDiv.html('<div id="videoHub-list">'+
								'<div id="videoHub-list-container" style="visibility:hidden"></div>'+
								'</div>'+
							   	'<div id="videoHub-current">'+
								'<div id="videoHub-current-container"></div>'+
								'</div>'+
								'<div id="videoHub-current-title"></div>'+
								'<div id="videoHub-current-info"></div>'+
								'<div id="videoHub-current-embed" style="display:none;"></div>'+
								'<div id="videoHub-current-email" style="display:none;"></div>');
				this.targetCurrent = $("#videoHub-current-container");
				this.targetList = $("#videoHub-list-container");
			},
			replaceVideo:function(libraryName, vidID) {
				//Retrive video
				var self = videoHubDB.library[libraryName].page[vidID];
				var videoSWF = self.media$group.media$content[0].url;
				videoSWF = videoSWF.replace(/\?.*/gi,"");
				
				var videoType = self.media$group.media$content[0].type;
				var vidTitle = self.title.$t;
				
				var videoDate = new Date();
				videoDate.setISO8601(self.media$group.yt$uploaded.$t);
				var relativeDate = relative_time(videoDate);
				if (self.yt$statistics) {
					var viewCount = self.yt$statistics.viewCount;
				} else {
					var viewCount = 0;
				}
				
				var vidLink = self.link[0].href;
				
				var vidInfo = self.media$group.media$credit.last().$t+'<br/>'+relativeDate+'<br/>'+viewCount+' views';
				var embedData = '<object width="275" height="226"><param name="movie" value="'+videoSWF+'"></param><param name="wmode" value="transparent"></param><embed src="'+videoSWF+'" type="'+videoType+'" width="275" height="226" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
				
				var emailData = 'mailto:?subject=Check%20Out%20This%20Cool%20Video%20I%20Found%20On%20The%20P2P%20Global%20Hub&body=Check%20out%20this%20video%20I%20found%20on%20the%20P2P%20Global%20Hub.%20'+vidLink;
				
				//Set Title
				$("#videoHub-current-title").html(vidTitle);
				
				//Set Video Info
				$("#videoHub-current-info").html(vidInfo);
				
				//Set Email Info
				if ($("#videoHub-button-share").length) {
					$("#videoHub-button-share").click(function(){
						videoHubController.sendEmail(emailData);									   
					});
				}				
					
				//set embed code
				$("#videoHub-current-embed").html('<textarea style="width:280px; height:260px; margin-top:10px;">'+embedData+'</textarea><div>Copy the above code</div>');
				
				
				//Set page SWF object
				this.targetCurrent.html('<div style="text-align:center;">'+embedData+'</div>');	
			},
			
			seedList: function(libraryName, startID) {
				var spaceFixer = new RegExp(" ", "gi");
				var divSuffix = libraryName.replace(spaceFixer,"")
				//console.log(divSuffix);
				//Create accordian div if doesn't exist
				if ($("#accordian-"+divSuffix).length) {
					//console.log("accordian exists");
				} else {
					//console.log("accordian doesn't exist");
					videoHubView.targetList.append('<h3><a href="#">'+libraryName+'</a></h3>'+
												  	'<div id="accordian-'+divSuffix+'">'+
													'<!--<div class="previous-button"></div>-->'+
													'<div class="vidBucket"></div>'+
													'<div class="next-button"></div>'+
													'</div>');
				}
				
				//$("#accordian-"+divSuffix+" .vidBucket").empty();
				for (var i=startID; (i < eval(parseInt(startID)+parseInt(videoHubDB.settings.videosPerTab))) && (i < videoHubDB.library[libraryName].page.length); i++) {
					var self = videoHubDB.library[libraryName].page[i];
					if (self.media$group.media$thumbnail) {
						var videoThumb = self.media$group.media$thumbnail[0].url;
					} else {
						continue;
					}
					var videoAuthor = self.media$group.media$credit.last().$t;
					
					
					$("#accordian-"+divSuffix+" .vidBucket").append('<table border="0" cellpadding="0" cellspacing="0">'+
																	'<tr><td style="padding-right:10px; padding-bottom:20px;" valign="top"><a href="javascript:videoHubView.replaceVideo(\''+libraryName+'\',\''+i+'\')"><img border="0" width="60px" src="'+videoThumb+'"/></a></td>'+
																	'<td style="font-size:8pt; padding-bottom:20px;" valign="top"><a class="videoLink" href="javascript:videoHubView.replaceVideo(\''+libraryName+'\',\''+i+'\')">'+self.title.$t+'</a><br/>by '+videoAuthor+'</td>'+
																	'</tr>'+
																	'</table>');
				}
				this.createButtons(libraryName, startID, divSuffix);
				
				this.drawAccordion();
			},
			drawAccordion:function() {
				if (videoHubView.libsLoaded == videoHubDB.libsTotal && !videoHubView.accordionFlag) {
					//initialize the accordian
					videoHubView.accordionFlag = "1";
					videoHubView.targetList.accordion({fillSpace:true, animated:false}).css("visibility", "visible");
					videoHubView.replaceVideo(videoHubDB.defaultLib, 0);
				}
			},
			
			createButtons: function(libraryName, startID, divSuffix) {
				var next = eval(parseInt(startID)+parseInt(videoHubDB.settings.videosPerTab));
				if (videoHubDB.library[libraryName].page.length > (next)) {
					$("#accordian-"+divSuffix+" .next-button").html("<a href=\"javascript:videoHubView.seedList('"+libraryName+"',"+next+");\">More</a>");	
				} else {
					$("#accordian-"+divSuffix+" .next-button").empty();	
				}
				
				/*var last = eval(parseInt(startID)-parseInt(videoHubDB.settings.videosPerTab));
				if (videoHubDB.library[libraryName].page[last]) {
					$("#accordian-"+divSuffix+" .previous-button").html("<a href=\"javascript:videoHubView.seedList('"+libraryName+"',"+last+");\">Less</a>");	
				} else {
					$("#accordian-"+divSuffix+" .previous-button").empty();
				}*/
			}
			
		}
		
	}
	
	videoHubView.init();
	
	//Create global videoHubDB once
	if (typeof(videoHubDB)!="object") {
		videoHubDB = {
			//load settings 
			defaultLib:"",
			libsTotal:this.libraries.length,
			settings:this.settings,
			library:{},
			//adds more entries into the videoHubDB
			addDataToLibrary:function(libInfo) {
				if (libInfo.url) {
					var url = libInfo.url;
				} else {
					var url = "http://gdata.youtube.com/feeds/api/playlists/"+libInfo.playlist+"?alt=json-in-script&max-results="+this.settings.videoGrab+"&start-index=1&format=5&safeSearch=strict&orderby=published&lr=en&v=2"
					//var url = "http://gdata.youtube.com/feeds/api/videos?alt=json-in-script&category=test&max-results="+this.settings.videosPerTab+"&start-index=1&format=5&safeSearch=strict&orderby=published&lr=en&v=2"
				}
				
				$.ajax({
					type:"GET",
					dataType:"jsonp",
					libInfo:libInfo,
					url:url,
					notNull:true,
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$('<p></p>')
						.html("There was an error with your request")
						.appendTo('body');
						returnVal = false;
					},
					success: function(html) {
						videoQuery = eval(html);
						if (typeof(videoQuery.feed.entry) == "object") {  //make sure that at least 1 entry is returned
							if (videoHubDB.defaultLib == "") {
								videoHubDB.defaultLib = this.libInfo.name;
							}
							
							if (videoQuery.feed.link[5]) {
								var nextURL = videoQuery.feed.link[5].href;	
							} else {
								var nextURL = "";	
							}
							videoHubDB.library[this.libInfo.name] = {
								info:this.libInfo,
								current:0,
								nextURL:nextURL,
								page:new Array()
							}
							
							for (var i = 0; i < videoQuery.feed.entry.length; i++) {
								videoHubDB.library[this.libInfo.name].page.push(videoQuery.feed.entry[i]);
							}
						} else {
							this.notNull = false;
						}
					},
					complete: function() {
						if (this.notNull) {
							videoHubView.libsLoaded++;
							videoHubView.seedList(this.libInfo.name, videoHubDB.library[this.libInfo.name].current);
						} else {
							videoHubDB.libsTotal--;
							videoHubView.drawAccordion();
						}
					}
				});
				
				
			}  
		} // end of creation of global DB object
	}
	
	//fetch all data based off of the items cued in this.libraries Array
	for (var i=0; i < this.libraries.length; i++) {
		videoHubDB.addDataToLibrary(this.libraries[i])
		
	}	
};


////////////////////////////
////////////////////////////
////////////////////////////
////////////////////////////



